package com.mbti.youmore.controller;

import com.mbti.youmore.common.exception.PostNotExistException;
import com.mbti.youmore.entity.Coupon;
import com.mbti.youmore.entity.Post;
import com.mbti.youmore.entity.UserCoupons;
import com.mbti.youmore.entity.resp.ResponseData;
import com.mbti.youmore.service.CouponService;
import com.mbti.youmore.util.AccessTokenUtils;
import com.mbti.youmore.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RequestMapping(path = "/coupon")
@RestController
public class
CouponController {

    @Autowired
    private CouponService couponService;
    //发放优惠券
    @RequestMapping(path = "/save", method = RequestMethod.POST)
    public ResponseData saveCoupon(@RequestBody Coupon coupon) {
        int result=0;
        result = couponService.saveCoupon(coupon);
        if (result == 0) {
            return ResponseUtil.getErrorResponse(501); //更新数据库失败
        }
        return ResponseUtil.getSuccessResponse(result);
    }
//    兑换优惠券
    @RequestMapping(path = "/exchange", method = RequestMethod.POST)
    public ResponseData exchangeCoupon(@RequestParam("userId") Integer userId,@RequestParam("couponId") Integer couponId) {
        int result=0;
        result = couponService.exchangeCoupon(userId,couponId);
        int result2=couponService.insert(userId,couponId);
//        UserCoupons userCoupons=new UserCoupons();
//        userCoupons.setUserId(userId);
//        userCoupons.setCouponId(couponId);

        if (result == 0) {
            return ResponseUtil.getErrorResponse(501); //更新数据库失败
        }
        return ResponseUtil.getSuccessResponse(result);
    }
//    获取可以兑换优惠券列表
@RequestMapping(method = RequestMethod.GET)
public ResponseData getCoupun(@RequestParam("userId") Integer userId) {
    List<Coupon> results = null;
    if (userId != null) {
            results = couponService.CouponList(userId);
    }
    return ResponseUtil.getSuccessResponse(results);
}
}